
using System;
using Qdb.Foundation;
using Qdb.Internal;
using Qdb.Query;

namespace Qdb.Config
{
	public class JavaSupport : IConfigurationItem
	{
		public void Prepare(IConfiguration config)
		{
			AddQdbBasicAliases(config);
			AddExceptionAliases(config);
			AddQueryAliases(config);
			AddCollectionAliases(config);
			AddClientServerAliases(config);
			
			config.ObjectClass("java.lang.Class").Translate(new TType());
		}

		private static void AddQdbBasicAliases(IConfiguration config)
		{
			config.AddAlias(new WildcardAlias("com.qdb.ext.*", "Qdb.Ext.*, Qdb"));
			config.AddAlias(new TypeAlias("com.qdb.StaticField", FullyQualifiedName(typeof(StaticField))));
			config.AddAlias(new TypeAlias("com.qdb.StaticClass", FullyQualifiedName(typeof(StaticClass))));
		}

		private static void AddCollectionAliases(IConfiguration config)
		{
			config.AddAlias(new TypeAlias("com.qdb.foundation.Collection4", FullyQualifiedName(typeof(Collection4))));
			config.AddAlias(new TypeAlias("com.qdb.foundation.List4", FullyQualifiedName(typeof(List4))));
		}

		private static void AddClientServerAliases(IConfiguration config)
		{
#if !SILVERLIGHT
			config.AddAlias(new TypeAlias("com.qdb.User", FullyQualifiedName(typeof(User))));
			config.AddAlias(new TypeAlias("com.qdb.cs.internal.messages.MUserMessage$UserMessagePayload", "Qdb.CS.Internal.Messages.MUserMessage+UserMessagePayload, Qdb.CS"));
			config.AddAlias(new TypeAlias("com.qdb.internal.TransportObjectContainer$KnownObjectIdentity", "Qdb.Internal.TransportObjectContainer+KnownObjectIdentity, Qdb"));
			config.AddAlias(new WildcardAlias("com.qdb.cs.internal.*", "Qdb.CS.Internal.*, Qdb.CS"));
#endif
		}

		private static void AddQueryAliases(IConfiguration config)
		{
			config.AddAlias(new TypeAlias("com.qdb.query.Evaluation", FullyQualifiedName(typeof(IEvaluation))));
			config.AddAlias(new TypeAlias("com.qdb.query.Candidate", FullyQualifiedName(typeof(ICandidate))));
			config.AddAlias(new WildcardAlias("com.qdb.internal.query.processor.*", "Qdb.Internal.Query.Processor.*, Qdb"));
		}

		private static void AddExceptionAliases(IConfiguration config)
		{
			config.AddAlias(new TypeAlias("com.qdb.foundation.ChainedRuntimeException", FullyQualifiedName(typeof(Exception))));
			config.AddAlias(new TypeAlias("java.lang.Throwable", FullyQualifiedName(typeof(Exception))));
			config.AddAlias(new TypeAlias("java.lang.RuntimeException", FullyQualifiedName(typeof(Exception))));
			config.AddAlias(new TypeAlias("java.lang.Exception", FullyQualifiedName(typeof(Exception))));
		}

		private static string FullyQualifiedName(Type type)
		{
			return ReflectPlatform.FullyQualifiedName(type);
		}

		public void Apply(IInternalObjectContainer container)
		{
		}
	}
}
